﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DAL
{
    public class D_Admin
    {
        public D_Admin()
        { }

        //管理员端登录
        public bool ValidateUser(Models.Admin_1 user)
        {
            try
            {
                var log = SqlSugarHelper.Db.Queryable<Admin_1>().Where(a => a.admin_uname == user.admin_uname && a.pwd == user.pwd).ToList();
                if (log.Count > 0)
                {
                    LogInfo.admin.admin_uname = user.admin_uname;
                    var sys_config = SqlSugarHelper.Db.Queryable<Sys_config>().ToList();

                    if (sys_config.Count > 0)
                    {
                        LogInfo.Sys_Config.tearch_choice = sys_config[0].tearch_choice;
                        LogInfo.Sys_Config.group_choice = sys_config[0].group_choice;
                    }

                    return true;
                }

                return false;

            }
            catch (SqlSugar.SqlSugarException ex)
            {
                // 处理数据库异常
                Console.WriteLine("数据库异常：" + ex.Message);
                return false;
            }
            catch (Exception ex)
            {
                //处理其他类型的异常
                Console.WriteLine("发生异常：" + ex.Message);
                return false;
            }
        }

        //导入管理员数据
        public bool add_A(List<Admin_1> admins)
        {
            var res = SqlSugarHelper.Db.Storageable(admins).ExecuteCommand();
            return res == admins.Count ? true : false;
        }

        //删除管理员数据
        public bool del_A(List<Admin_1> admins)
        {
            var res = SqlSugarHelper.Db.Deleteable(admins).ExecuteCommand();
            return res == admins.Count ? true : false;
        }

        //保存管理员数据修改
        public bool save_List_A(List<Admin_1> admins)
        {
            var count = SqlSugarHelper.Db.Storageable<Admin_1>(admins).ExecuteCommand();
            return count == admins.Count ? true : false;
        }

        //导入管理员数据页面的换页功能
        public List<Admin_1> getPage_A(int Page_idx, int Page_size, ref int totalCount)
        {
            return SqlSugarHelper.Db.Queryable<Admin_1>().ToPageList(Page_idx, Page_size, ref totalCount);
        }

        //查询学生数据
        public List<Student> getList_S()
        {
            var list = SqlSugarHelper.Db.Queryable<Student>().ToList();
            return list;
        }

        //导入学生数据
        public bool add_S(List<Student> students)
        {
            var res = SqlSugarHelper.Db.Storageable(students).ExecuteCommand();
            return res == students.Count ? true : false;
        }

        //删除学生数据
        public bool del_S(List<Student> students)
        {
            var res = SqlSugarHelper.Db.Deleteable(students).ExecuteCommand();
            return res == students.Count ? true : false;
        }

        //保存学生数据修改
        public bool save_List_S(List<Student> students)
        {
            var count = SqlSugarHelper.Db.Storageable<Student>(students).ExecuteCommand();
            return count == students.Count ? true : false;
        }

        //导入学生数据页面的换页功能
        public List<Student> getPage_S(int Page_idx, int Page_size, ref int totalCount)
        {
            return SqlSugarHelper.Db.Queryable<Student>().ToPageList(Page_idx, Page_size, ref totalCount);
        }

        //导入老师数据
        public bool add_T(List<Teacher> teachers)
        {
            var res = SqlSugarHelper.Db.Storageable(teachers).ExecuteCommand();
            return res == teachers.Count ? true : false;
        }

        //删除老师数据
        public bool del_T(List<Teacher> teachers)
        {
            var res = SqlSugarHelper.Db.Deleteable(teachers).ExecuteCommand();
            return res == teachers.Count ? true : false;
        }

        //保存老师数据修改
        public bool save_List_T(List<Teacher> teachers)
        {
            var count = SqlSugarHelper.Db.Storageable<Teacher>(teachers).ExecuteCommand();
            return count == teachers.Count ? true : false;
        }

        //查询老师数据
        public List<Teacher> getList_T()
        {
            var list = SqlSugarHelper.Db.Queryable<Teacher>().ToList();
            return list;
        }

        //导入老师数据页面的换页功能
        public List<Teacher> getPage_T(int Page_idx, int Page_size, ref int totalCount)
        {
            return SqlSugarHelper.Db.Queryable<Teacher>().ToPageList(Page_idx, Page_size, ref totalCount);
        }

        //查询学生组信息
        public List<StuGroup> getList_G()
        {
            var list = SqlSugarHelper.Db.Queryable<StuGroup>().ToList();
            return list;
        }

        //保存学生组信息
        public bool save_List_G(List<StuGroup> groups)
        {
            var count = SqlSugarHelper.Db.Storageable<StuGroup>(groups).ExecuteCommand();
            return count == groups.Count ? true : false;
        }

        //按老师ID排序查询学生组信息
        public List<StuGroup> getList_S_T()
        {
            var list = SqlSugarHelper.Db.Queryable<StuGroup>().OrderBy(s_g => s_g.teacher_id).ToList();
            return list;
        }

        //学生表和学生组表连表查询
        public List<Student> getList_S_G(Models.StuGroup group)
        {
            //学生表的组id==组表的组id
            var list = SqlSugarHelper.Db.Queryable<Student>().Where(stu => stu.group_id == group.group_id).ToList();
            return list;
        }

        //学生组自动分配结果保存
        public bool save_result_S(List<StuChoice> gs)
        {
            var count = SqlSugarHelper.Db.Storageable<StuChoice>(gs).ExecuteCommand();
            return count == gs.Count ? true : false;
        }

        //学生组选择结果查询
        public List<Models.StuChoice> getList_S_C()
        {
            var list = SqlSugarHelper.Db.Queryable<Models.StuChoice>().ToList();
            return list;
        }

        //老师自动分配结果保存
        public bool save_result_T(List<Models.TeaChoice> ts)
        {
            var count = SqlSugarHelper.Db.Storageable<TeaChoice>(ts).ExecuteCommand();
            return count == ts.Count ? true : false;
        }

        //老师选择结果查询
        public List<Models.TeaChoice> getList_T_C()
        {
            var list = SqlSugarHelper.Db.Queryable<TeaChoice>().ToList();
            return list;
        }
    }
}
